<?php
class PNVarifyUserTopicsArray extends PNObjectExArray {
  function PNVarifyUserTopicsArray($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'innoforum_topics';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';
    //$this->_objWhere      = "tpc_cr_uid = '2'";
    $this->_objColumnExArray = array( 'is_new'      => "IF ( DATEDIFF(DATE(tpc_cr_date),DATE(NOW())) <1 
                                                             AND
                                                             DATEDIFF(DATE(tpc_cr_date),DATE(NOW())) >-3,
                                                            'T','F')");
/*
    $this->_objColumnExArray = array( 'is_update'      => "IF ( DATEDIFF(DATE(tpc_lu_date),DATE(NOW())) <1,
                                                            'T','F')");
*/

    $this->_objJoin[]     = array ( 'join_table'  =>  'group_membership',
                            'join_field'          =>  array('gid'),
                            'object_field_name'   =>  array('group_id'),
                            'compare_field_table' =>  'cr_uid',
                            'compare_field_join'  =>  'uid');

/*
    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_category',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('category_name'),
                            'compare_field_table' =>  'category_id',
                            'compare_field_join'  =>  'id');
*/
/*
    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_subcategory',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('subcategory_name'),
                            'compare_field_table' =>  'subcategory_id',
                            'compare_field_join'  =>  'id');
*/
/*

    $this->_objJoin[]     = array ( 'join_table'  =>  'innoforum_posts',
                            'join_field'          =>  array('id'),
                            'object_field_name'   =>  array('post_id'),
                            'compare_field_table' =>  'id',
                            'compare_field_join'  =>  'topics_id');
    */
    $this->_init($init, $where);
  }
  function genSort(){
    return 'ORDER BY tpc_obj_status,tpc_temp_cr_date DESC';  
  }

  function genFilter(){
    $filter  = FormUtil::getPassedValue ('filter', false);
    $keyword = DataUtil::formatForStore($filter['keyword']);
    $type = DataUtil::formatForStore($filter['type']);

    $category_id = DataUtil::formatForStore($filter['category_id']);
    $subcategory_id = DataUtil::formatForStore($filter['subcategory_id']);
    $type_id = DataUtil::formatForStore($filter['type_id']);

    $wheres = array();
    //filter by user_id
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    if ($user_id) $wheres[] = "tpc_cr_uid = '$user_id'";
    //filter by keyword
    if ($keyword && $type){
      switch($type){
        case 'name': $wheres[] = "tpc_name LIKE '%". $keyword ."%'"; break;
        case 'title': $wheres[] = "tpc_title LIKE '%". $keyword ."%'"; break;
        case 'category_name': $wheres[] = "cty_name LIKE '%". $keyword ."%'"; break;
        case 'subcategory_name': $wheres[] = "scy_name LIKE '%". $keyword ."%'"; break;
      }
    }
    //filter by selector
    if ($category_id && intval($category_id)) $wheres[] = "tpc_category_id ='$category_id'";
    if ($subcategory_id && intval($subcategory_id)) $wheres[] = "tpc_subcategory_id ='$subcategory_id'";
    if ($type_id && intval($type_id)) $wheres[] = "tpc_type_id ='$type_id'";

    //filter only admin
    $wheres[] = " pn_gid = '6' || pn_gid = '5' || pn_gid = '7' || pn_gid = '8' || tpc_cr_uid = '2' ";
    //$wheres[] = "tpc_cr_uid <> '2' ";
    //filter not sold out
    //$wheres[] = "tpc_type_id <> '3' ";
    return implode(' AND ', $wheres) ;
  }
  function selectExtendResult(){
    $user_id = DataUtil::formatForStore(FormUtil::getPassedValue ('user_id', false));
    $result = array();
    if ($user_id){
      $result['user'] = InnoUtil::getUserInfo($user_id);
    }
    return $result;
  }
}